package com.food.chronicles.user.config;

import org.springframework.stereotype.Service;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

@Service
public class LoginFilter implements Filter {

    private static final Set<String> ALLOW_ORIGINS = new HashSet<>();

    //排除的url
    private static final Set<String> EXCLUDE_URLS = new HashSet<>();

    private static final String LOGIN_URI = "http://localhost:8080/";

    static {
        EXCLUDE_URLS.add("/user/login.json");
        EXCLUDE_URLS.add("/common/tradingdirectiontypes.json");
        EXCLUDE_URLS.add("/common/transactiontypes.json");
        EXCLUDE_URLS.add("/user/getuserbykeywords.json");
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//        //走登陆是要种cookie
//        //其他的url是要检查cookie
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        allowCORS(request,response);
//        //设置domain
//        String domain = DomainUtil.getDomain(request);
//        System.out.println("domain = " + domain);
//        ThreadLocalUtil.getInstance().set(Conts.ROOT_DOMAIN,domain);
//        //获取url
//        String servletPath = request.getServletPath();
//        if(!needFilter(servletPath)){
//            filterChain.doFilter(servletRequest, servletResponse);
//            return;
//        }
//        Cookie cookie = CookieUtil.getCookie(request, Conts.CURRENT_USER_ID);
//        if(cookie == null){
//            //重定向或者啥玩意的   给我重定向
//            response.sendError(SC_UNAUTHORIZED);
//            return;
//        }
//        //
//        if(Conts.CURRENT_USER_ID.equals(cookie.getName())){
//            ThreadLocalUtil.getInstance().set(Conts.CURRENT_USER_ID,cookie.getValue());
//        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }

    private void allowCORS(HttpServletRequest request, HttpServletResponse response) {
        String originHeader = request.getHeader("Origin");
//        if (originHeader != null && ALLOW_ORIGINS.contains(originHeader)) {
            response.setHeader("Access-Control-Allow-Origin", originHeader);
            response.setHeader("Access-Control-Allow-Methods", "GET,POST,DELETE,OPTIONS");
            response.setHeader("Access-Control-Allow-Headers", " Content-Type, Authorization, Accept");
//            response.setHeader("Access-Control-Allow-Headers", "X-Alt-Referer,X-Requested-With");
            response.setHeader("Access-Control-Allow-Headers", "x-requested-with, content-type");
            response.setHeader("Access-Control-Allow-Credentials", "true");
//        }
    }

//    private boolean needFilter(String requestUri) {
//        if (StringUtils.isEmpty(requestUri)) {
//            return true;
//        }
//        AntPathMatcher matcher = new AntPathMatcher();
//        for (String pattern : EXCLUDE_URLS) {
//            if (matcher.match(pattern, requestUri)) {
//                return false;
//            }
//        }
//        return true;
//    }
}
